<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>房间管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/js/lay-module/dtree/dtree.css}">
    <link rel="stylesheet" th:href="@{/layuimini/js/lay-module/dtree/font/dtreefont.css}">

</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon layui-icon-addition"></i>新增房间</button>
            </div>
        </script>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
        </script>

    </div>
</div>

<div id="dataFormModal" style="display: none;padding: 10px">
    <form class="layui-form layuimini-form" id="dataForm" lay-filter="dataFormFilter">

        <input type="hidden" name="id">

        <div class="layui-form-item">
            <label class="layui-form-label required">房间名称</label>
            <div class="layui-input-block">
                <input type="text" name="roomName" lay-verify="required" placeholder="请输入房间名称" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">房间类型</label>
            <div class="layui-input-block">
                <input type="text" name="roomType" lay-verify="required" placeholder="请输入房间类型（大包，中包或者小包" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">房间号</label>
            <div class="layui-input-block">
                <input type="text" name="roomNumber" lay-verify="required" placeholder="请输入房间号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">房间价格</label>
            <div class="layui-input-block">
                <input type="text" name="roomPrice" lay-verify="required" placeholder="请输入房间价格" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="dataFormSubmit">保存</button>
                <button type="button" class="layui-btn layui-btn-primary" id="dataFormReset">取消</button>
            </div>
        </div>
    </form>
</div>

<!--xml-select 解决layui多选的解决方案(从后端拿数据)-->
<script th:src="@{/layuimini/js/lay-module/xm-select/xm-select.js}"></script>
<script th:src="@{/layuimini/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/layuimini/js/lay-config.js?v=2.0.0}" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','dtree'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        let active = {
            dataModelType: 1,
            openDataFormModal() {
                let title = active.dataModelType == 1 ? '添加新房间' : '编辑相关资料';
                layer.open({
                    title: title
                    , type: 1
                    , content: $('#dataFormModal')
                    , area: ['650px', '350px']
                    , maxmin: true
                    , cancel: function () {
                        active.closeDataFormModal();
                    }
                });
            },
            closeDataFormModal() {
                $('#dataForm')[0].reset();
                layer.closeAll();
            }
        };

        table.render({
            elem: '#currentTableId',
            method: 'post',
            url: '/system/roomInfo/listByTable',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            height: "full-" + 86,
            page: true,
            limit: 15,
            limits: [15, 20, 25],
            cols: [ [
                {type: "checkbox", width: 50},
                {type: 'numbers',title: '序号'},
                {field: 'roomName', width: 400, title: '房间名称'},
                {field: 'roomType', title: '房间类型'},
                {field: 'roomNumber', title: '房间号'},
                {field: 'roomPrice', title: '房间价格(单位：元)'},
                {title: '操作', width: 140,align:"center", toolbar: '#currentTableBar', align: "center"}
            ]]
        });

        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                active.dataModelType = 1;
                active.openDataFormModal();
            }
        });

        /**
         * 编辑、删除
         */
        table.on('tool(currentTableFilter)',function (obj) {
            let data = obj.data;
            if(obj.event === 'edit'){
                //显示当前行的数据
                active.dataModelType = 2;
                form.val('dataFormFilter',data);
                active.openDataFormModal();
            }
        });

        // 监听 add表单 关闭
        $('#dataFormReset').on('click', function () {
            active.closeDataFormModal();
        });

        //监听表单提交
        form.on('submit(dataFormSubmit)', function (data) {
            $.ajax({
                type: 'post',
                url: active.dataModelType == 1 ? '/system/roomInfo/save' : '/system/roomInfo/updateById',
                dataType: 'json',
                data: data.field,
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {time: 500, icon: 1}, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {time: 500, icon: 2});
                    }
                },
                error: function (res) {
                    layer.msg("请求失败", {time: 500, icon: 2});
                }
            });
            return false;
        });

    });
</script>

</body>
</html>